package com.Team7.controller;

import com.Team7.call.AddFileCall;
import com.Team7.call.AddMenuCall;
import com.Team7.domain.Item;
import com.Team7.factory.DiskFactory;
import com.Team7.util.DiskUtil;
import com.Team7.util.FileUtil;
import com.Team7.util.StringUtil;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class MenuController {

    private Stage stage;
    private AddMenuCall addMenuCall;
    private String curPath;

    @FXML
    private TextField tf_MenuName;

    @FXML
    private Button btn_createMenu;


    public MenuController(AddMenuCall addMenuCall,String curPath){
        this.stage=new Stage();
        this.addMenuCall=addMenuCall;
        this.curPath=curPath;
        try{
            FXMLLoader fxmlLoader=new FXMLLoader(getClass().getResource("../fxml/AddMenu.fxml"));
            fxmlLoader.setController(this);
            Scene scene=new Scene(fxmlLoader.load(),300,150) ;
            stage.setScene(scene);
            stage.setTitle("添加文件夹");
        }catch (Exception e){
            e.printStackTrace();
        }
    }



    public void show(){
        this.stage.show();
    }

    @FXML
    private void initialize(){
        btn_createMenu.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("123");

                String filename=tf_MenuName.getText().trim();

                String absolatePath= StringUtil.getAbsolutePath(curPath,filename);
                Boolean success=FileUtil.createFile(DiskFactory.getDisk(),absolatePath,(byte) 8);
                //第一个初始化为 #
                //DiskFactory.getDisk()[bytes[6]][0]=35;

                if(success){
                    addMenuCall.addMenu(filename);
                    stage.close();
                }else{
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("Information Dialog");
                    alert.setHeaderText(null);
                    alert.setContentText("File has been created ,you have to change another name!");
                    alert.showAndWait();
                }

            }
        });

    }

}


/* byte[] name=(tf_MenuName.getText().trim()).getBytes();
                Item item=new Item();
                byte[] bytes=new byte[8];
                for(int i=0;i<3;i++){
                    if(name.length<i+1){
                        bytes[i]=0;
                    }else{
                        bytes[i]=name[i];
                    }
                }
                //类型
                bytes[3]=0;
                bytes[4]=0;
                //属性
                bytes[5]=8;
                //起始盘块
                bytes[6]=(byte) DiskUtil.getFreeBlock(DiskFactory.getDisk());
                //长度
                bytes[7]=0;
                item.setAttr(bytes);
                addMenuCall.addMenu(item);
                stage.close();*/